--require "MainScene"

JLZMLayer={}
JLZMLayerFunc={}
JLZMLayerFunc.__index = JLZMLayerFunc
setmetatable(JLZMLayer, JLZMLayerFunc)

JLZMLayer.NodeID = {HeroTable=1001}
local HeroItemID = {ImgBK=2001, Name=2011, Level=2012, Button1=2021, Button2=2022}

JLZMLayerFunc.getNode = function()
  local l = {}
  setmetatable(l, JLZMLayerFunc)
  l.node = CCBReader:load("JunXiaoLayer1.ccbi")
  print("JLZMLayer.getNode", JLZMLayer.node)
  --l.node:registerScriptTouchHandler(onTouchJLZMLayer, false, 0, true)
  --l.node:setTouchEnabled(true)
  JLZMLayer.instance = l
  l:init() 
  return l 
end

JLZMLayerFunc.getScene = function(self)
  local sc = CCScene:create()
  sc:addChild(JLZMLayer:getNode().node)  
  print("JLZMLayer.getScene", sc)
  return sc
end

JLZMLayerFunc.init = function(self)
  local lb = self.node:getChildByTag(1011)
  local count=0
  for k,v in pairs(UserData.userinfo.host.armyGroups) do count=count+1; end
  TypeCast(lb, "CCLabelTTF"):setString(string.format("军校将领：%d/20", count))
  self:initTable()
end

JLZMLayerFunc.initTable = function(self)
  --local heros = UserData.userinfo.host.store.refresh.g
  if self.table~=nil then
    self.node:removeChild(self.table, true)
  end
  local armys = UserData.userinfo.host.armyGroups
  --[[local sorted = {}
  for k,v in pairs(armys) do
    sorted[#sorted+1] = {k=k, v=v}
  end
  local sortFunc = function(a, b)
    local infoa = UserData.config.models.General[tonumber(a.k)]
    local infob = UserData.config.models.General[tonumber(b.k)]
    if a.up>b.up then
      return true
    elseif a.up<b.up then
      return false
    else
      return a.v.l>b.v.l
    end
  end]]

  self.buttons = {}
  print("JLZMLayerFunc.initTable initArmygroupTable armys", armys)
  local items = CCArray:create()
  for k,v in pairs(armys) do
    print("JLZMLayerFunc.initTable initArmygroupTable", k, v)
    local item = CCBReader:load("JiangLingItem1.ccbi")
    local info = UserData.config.models.General[tonumber(k)]
    --local skillName = UserData.config.models.NewSkill[info.skill].name
    --local imgName = string.format("%d.png",k%40)
    local imgName = string.format("%s_100.png",info.asset)
    local imgBK = item:getChildByTag(HeroItemID.ImgBK)
    local button = CCButton:createWithFileName("onClickHero", imgName, imgName, imgName, "")
    button:setTag(tonumber(k))
    item:addChild(button)
    setObjectExtTable(button, self)
    button:setPosition(imgBK:getPosition())
    
    local lb = item:getChildByTag(HeroItemID.Name) 
    TypeCast(lb, "CCLabelTTF"):setString(info.name)
    TypeCast(lb, "CCLabelTTF"):setColor(Equip.EquipColor[info.quality])

    lb = item:getChildByTag(HeroItemID.Level)
    TypeCast(lb, "CCLabelTTF"):setString(string.format("Lv%d", v.l))
 
    local button = nil 
    local tag = nil
    if v.s>0 then
      title="下野"
      tag = 200000 + tonumber(k)
      button = CCButton:createWithFileName("onZhaoMuButton", "btn_blue_normal.png", "btn_blue_highlight.png", "btn_disabled.png", title)
    else
      local title="招募"
      tag = 100000 + tonumber(k)  
      button = CCButton:createWithFileName("onZhaoMuButton", "btn_green_normal.png", "btn_green_highlight.png", "btn_disabled.png", title)
    end
    button:setPosition(ccp(226, 136))
    setObjectExtTable(button, self)
    item:addChild(button)
    button:setTag(tag)
    self.buttons[#self.buttons+1] = button
    print("####", button)

    button = CCButton:createWithFileName("onQuZhuButton", "btn_red_normal.png", "btn_red_highlight.png", "btn_disabled.png", "驱逐")
    button:setPosition(ccp(226, 72))
    setObjectExtTable(button, self)
    item:addChild(button)
    button:setTag(300000+tonumber(k))
    
    item:setContentSize(CCSizeMake(320,190)) 
    items:addObject(item)
  end
  print("JLZMLayerFunc.initTable initArmygroupTable size", items:count())
  local tableBK = self.node:getChildByTag(JLZMLayer.NodeID.HeroTable)
  local size = tableBK:getContentSize()
  local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(x+10, y+15)
  self.node:addChild(tableview)
  self.table = tableview
  if self.oldTableOffset~=nil then
    TypeCast(self.table,"CCScrollView"):setContentOffset(self.oldTableOffset)
  end
end

JLZMLayerFunc.onTouchJLZMLayer = function(event, x, y)
  if event==CCTOUCHBEGAN then
    if JLZMLayer.instance.curTip~=nil then
      print("onTouchTipLayer clear tip")
      JLZMLayer.instance.node:removeChild(JLZMLayer.instance.curTip, true)
      JLZMLayer.instance.curTip = nil
    end
  end
  return true 
end

JLZMLayerFunc.onClickHero= function(self, node, event)
  print("JLZMLayerFunc.onHeroButton touched ", event, node:getTag())
  if self.curTip~=nil then
    self.node:removeChild(self.curTip, true)
  end
  local hd = HeroData:create(node:getTag())
  local tip = hd:createTip()
  self.curTip = tip
  self.node:addChild(self.curTip)
  self.curTip:registerScriptTouchHandler(JLZMLayerFunc.onTouchTipLayer, false, -1111, true)
  self.curTip:setTouchEnabled(true)
end

JLZMLayerFunc.onZhaoMuButton = function(self, node, event)
  print("JLZMLayerFunc.onZhaoMuButton", node:getTag())
  if math.floor(node:getTag()/100000)==1 then
    NetworkRequest.zhaomuGeneral(node:getTag()-100000, "onZhaoMuComplete", self)
  elseif math.floor(node:getTag()/100000)==2 then
    NetworkRequest.xiayeGeneral(node:getTag()-200000, "onZhaoMuComplete", self)
  end
  --setTouchEnabledAll(JunXiaoScene.instance.node, false)
  LoadScene:showLoadingScene()
  self.oldTableOffset = TypeCast(self.table,"CCScrollView"):getContentOffset()
  --self:initTable()
end
JLZMLayerFunc.onQuZhuButton = function(self, node, event)
  print("JLZMLayerFunc.onQuZhuButton", node:getTag())
  NetworkRequest.quzhuGeneral(node:getTag()-300000, "onZhaoMuComplete", self)
  --setTouchEnabledAll(JunXiaoScene.instance.node, false)
  LoadScene:showLoadingScene()
  self.oldTableOffset = TypeCast(self.table,"CCScrollView"):getContentOffset()
end
JLZMLayerFunc.onZhaoMuComplete = function(self, data)
  print("JLZMLayerFunc.onZhaoMuComplete", data)
  LoadScene:removeLoadingScene()
  --setTouchEnabledAll(JunXiaoScene.instance.node, true)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    --local heros = UserData.userinfo.host.store.refresh.g
    --heros[self.currentHeroIndex] = nil
    self:initTable()
    if JunXiaoScene.guide1==true then
      JunXiaoGuide.nextStep()
    end
  else
    showMessage(res.msg, ccc3(255,255,0))
  end  
end
